/* This is an unpublished work containing 3M confidential and proprietary
 * information. Disclosure, use or reproduction without the written
 * authorization of 3M is prohibited. If publication occurs, the following
 * notice applies:
 *      Copyright  2011, 3M. All rights reserved.
 */
package com.mmm.eoc.funtionaltests.selenium;

import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;


public class FirstSeleniumTest {

    @Before
    public void setup() {
        System.setProperty("webdriver.chrome.driver", "C:\\berg\\chromedriver.exe");
        System.setProperty("webdriver.firefox.bin", "C:\\Berg\\FirefoxPortable\\FirefoxPortable.exe");
    }


    @Test
    public void testWithIE() {

        WebDriver driver = new InternetExplorerDriver();
        doTest(driver);
    }

//    @Test
    public void testWithFireFox() {
        //C:\Berg\FirefoxPortable
//        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
//        capabilities.setCapability("firefox.binary", "C:\\Berg\\FirefoxPortable\\FirefoxPortable.exe");
        WebDriver driver = new FirefoxDriver();
        doTest(driver);
    }

    @Test
    public void testWithChrome() {
        WebDriver driver = new ChromeDriver();
        doTest(driver);
    }

    private void doTest(WebDriver driver) {
        // And now use this to visit Google
        driver.get("http://www.google.com");
        // Alternatively the same thing can be done like this
        // driver.navigate().to("http://www.google.com");

        // Find the text input element by its name
        WebElement element = driver.findElement(By.name("q"));

        // Enter something to search for
        element.sendKeys("Cheese!");

        // Now submit the form. WebDriver will find the form for us from the element
        element.submit();

        // Check the title of the page
        System.out.println("Page title is: " + driver.getTitle());

        // Google's search is rendered dynamically with JavaScript.
        // Wait for the page to load, timeout after 10 seconds
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                return d.getTitle().toLowerCase().startsWith("cheese!");
            }
        });

        // Should see: "cheese! - Google Search"
        System.out.println("Page title is: " + driver.getTitle());

        //Close the browser
        driver.quit();
    }
}
